package com.practice.utils;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.IdUtil;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 自动生成ID（雪花算法）
 *
 * @author Masiyu
 * @date 2023/9/26 15:27
 */
@Component
public class MyIdGenerator {
    private long workerId=0L;
    private long datacenterId=1L;
    private Snowflake snowflake= IdUtil.createSnowflake(workerId, datacenterId);
    @PostConstruct
    public void init(){
        try{
            workerId= NetUtil.ipv4ToLong(NetUtil.getLocalhostStr());
        }catch(Exception e){
            workerId=NetUtil.getLocalhostStr().hashCode();
        }
        
    }
    
    public synchronized long snowflakeId(long workerId,long datacenterId){
        Snowflake snowflake=IdUtil.createSnowflake(workerId, datacenterId);
        return snowflake.nextId();
    }
    public synchronized long snowflakeId(){
        return snowflake.nextId();
    }
}
